/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "fdcan.h"
#include "usbd_cdc_if.h"
#include "usart.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
unsigned int DATA_SEND_STATUS;
//USB_TRANS	USB_Data;
uint8_t status_tr;
/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 1024 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);

extern void MX_LWIP_Init(void);
extern void MX_USB_DEVICE_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* init code for LWIP */
 // MX_LWIP_Init();

  /* init code for USB_DEVICE */
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN StartDefaultTask */
    FDCAN_RxHeaderTypeDef fdcan_RxHeader;
    uint8_t data[8] = {0};
    memcpy(data,&time_count,sizeof(time_count));
    CAN1_SendMsg(fdcan_RxHeader.Identifier, FDCAN_STANDARD_ID, FDCAN_DLC_BYTES_8, (uint8_t *)data);

    /* Infinite loop */
  for(;;)
  {
      osDelay(500);
      memcpy(data,&time_count,sizeof(time_count));
      CAN1_SendMsg(fdcan_RxHeader.Identifier, FDCAN_STANDARD_ID, FDCAN_DLC_BYTES_8, (uint8_t *)data);

      //HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);

//      CDC_Transmit_HS(data,8);
//      if(status_tr == 2)
//      {
//          time_count;
//          CAN1_SendMsg(fdcan_RxHeader.Identifier, FDCAN_STANDARD_ID, FDCAN_DLC_BYTES_8, (uint8_t *)data);
//
//      }
  }
  /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
uint16_t i = 0;

uint8_t LINCalcParity(uint8_t id)
{
    uint8_t parity = id;
    uint8_t p0 = ((parity & 0x01) ^ ((parity >> 1) & 0x01) ^ ((parity >> 2) & 0x01) ^ ((parity >> 4) & 0x01)) << 6;
    uint8_t p1 = (~( ((parity >> 1) & 0x01) ^ ((parity >> 3) & 0x01) ^ ((parity >> 4) & 0x01) ^ ((parity >> 5) & 0x01) )) << 7;
    parity |= (p0 | p1);
    return parity;
}

uint8_t LINCalcChecksum(uint8_t *data, uint8_t datanum, uint8_t identifier)
{
    uint16_t sum = 0;
    uint8_t i;
    for(i = 0; i < datanum; i++)
    {
        sum += data[i];
        if(sum & 0xFF00)
            sum = (sum & 0x00FF) + 1;
    }
    sum += identifier;
    if(sum & 0xFF00)
        sum = (sum & 0x00FF) + 1;
    sum ^= 0x00FF;

    return ((uint8_t)sum);
}


void LIN1_Master_SendFrame(uint8_t identifier, uint8_t *data, uint8_t length)
{
    uint8_t linHead[2] = {0x55, 0x00};
    uint8_t linData[9];

    linHead[1] = LINCalcParity(identifier);

    for (int i = 0; i < length; i++)
    {
        linData[i] = data[i];
    }
    linData[i] = LINCalcChecksum(data, length, identifier);


    HAL_LIN_SendBreak(&huart4);
    HAL_UART_Transmit(&huart4, linHead, 2, 1000);
    HAL_UART_Transmit(&huart4, linData, length + 1, 1000);
}

void LIN2_Master_SendFrame(uint8_t identifier, uint8_t *data, uint8_t length)
{
    uint8_t linHead[2] = {0x55, 0x00};
    uint8_t linData[9];

    linHead[1] = LINCalcParity(identifier);

    for (int i = 0; i < length; i++)
    {
        linData[i] = data[i];
    }
    linData[i] = LINCalcChecksum(data, length, identifier);


    HAL_LIN_SendBreak(&huart5);
    HAL_UART_Transmit(&huart5, linHead, 2, 1000);
    HAL_UART_Transmit(&huart5, linData, length + 1, 1000);
}

void LIN3_Master_SendFrame(uint8_t identifier, uint8_t *data, uint8_t length)
{
    uint8_t linHead[2] = {0x55, 0x00};
    uint8_t linData[9];

    linHead[1] = LINCalcParity(identifier);

    for (int i = 0; i < length; i++)
    {
        linData[i] = data[i];
    }
    linData[i] = LINCalcChecksum(data, length, identifier);


    HAL_LIN_SendBreak(&huart7);
    HAL_UART_Transmit(&huart7, linHead, 2, 1000);
    HAL_UART_Transmit(&huart7, linData, length + 1, 1000);
}
/* USER CODE END Application */

